﻿

using OxyPlot.Axes;//import LinearAxis设置标签
using OxyPlot.Annotations;//上下限
using OxyPlot;
using OxyPlot.Series;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace WpfApp1
{


    public partial class PlotWindowViewModel: ObservableObject
    {

        public PlotModel MyPlotModel{get; set;}
        public PlotModel MyPlotModel2{get; set;}
        


        public PlotWindowViewModel()
        {
            
            MyPlotModel=new PlotModel{Title = "My Plot"};
            
            // 添加X轴并设置标签
            MyPlotModel.Axes.Add(new LinearAxis
            {
                Position = AxisPosition.Bottom, // 设置轴的位置在底部
                Title = "X Axis Label",         // 设置X轴标签
            });

            // 添加Y轴并设置标签
            MyPlotModel.Axes.Add(new LinearAxis
            {
                Position = AxisPosition.Left,    // 设置轴的位置在左侧
                Title = "Y Axis Label",          // 设置Y轴标签
            });    
            
            ////////////////////////////////////////////////////////////////////////////
            //该序列使用 Math.Sin 函数生成数据点，并且这些数据点将从 x = 0 到 x = 10 之间以 0.1 的步长进行采样。
            MyPlotModel.Series.Add(new FunctionSeries(Math.Sin, 0, 10, 0.1,"sin(x)"));
            
            ///////////////////////////////////////////////////////////////
            var series = new LineSeries
            {
                MarkerType = MarkerType.Circle,
                MarkerSize = 5,
                MarkerStroke = OxyColors.White
            };
            series.Points.Add(new DataPoint(0, 0));
            series.Points.Add(new DataPoint(1, 1));
            series.Points.Add(new DataPoint(2, 4));
            series.Points.Add(new DataPoint(3, 9));
            series.Points.Add(new DataPoint(4, 16));
            MyPlotModel.Series.Add(series);
            
            
            MyPlotModel2=new PlotModel{Title = "My Plot2"};
            Line2 = new LineSeries();
            //List<int> plist = [1, 2, 4, 4, 2, 7, 2, 4, 7, 2];
            //var points = plist.Select((value, index) => new DataPoint(index, value)).ToList();
            //Line2.Points.AddRange(points);
            MyPlotModel2.Series.Add(Line2);
            MyPlotModel2.Annotations.Add(new LineAnnotation
            {
                Color = OxyColors.Blue,
                Type = LineAnnotationType.Horizontal,
                Y = 0.8,
                Text = "Upper Limit",
                TextPosition = new DataPoint(double.NaN, 0.9),
                StrokeThickness = 2
            });
            
        }

        private LineSeries Line2 { get; set; }
        private bool _isRunning;

        
        [RelayCommand]
        private void UpdatePlot()
        {
            if (_isRunning==false)
            {
                Thread thread = new Thread(AddPoints);
                thread.Start();
                _isRunning=true;
            }
            else
            {
                _isRunning=false;
            }
        }

        private void AddPoints()
        {
            while (_isRunning)
            {
                //Random random = new Random();
                Line2.Points.Add(new DataPoint(Line2.Points.Count+1,Math.Sin(Line2.Points.Count*Math.PI/180)));
                MyPlotModel2.InvalidatePlot(true);// 强制图表重新计算布局和坐标轴范围
                Thread.Sleep(3);
            }

        }
    }
}

